﻿using MediatR;
using Microsoft.AspNetCore.Mvc;
using Sgr.Application.Queries;
using Sgr.AspNetCore.ActionFilters.AuditLogs;
using Sgr.AspNetCore.ActionFilters.Permissions;
using Sgr.UPMS.Application.Commands.Organizations;
using Sgr.UPMS.Application.Queries;
using Sgr.UPMS.Application.ViewModels;

namespace Sgr.UPMS.Controllers
{
    /// <summary>
    /// 组织邀请管理
    /// </summary>
    [Route("api/orgs/invitations")]
    [ApiController]
    [Produces("application/json")]
    [Tags("OrganizationInvitation")]
    public class OrganizationInvitationController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly IOrganizationQueries _organizationQueries;

        public OrganizationInvitationController(
            IMediator mediator,
            IOrganizationQueries organizationQueries)
        {
            _mediator = mediator ?? throw new ArgumentNullException(nameof(mediator));
            _organizationQueries = organizationQueries ?? throw new ArgumentNullException(nameof(organizationQueries));
        }

        /// <summary>
        /// 创建邀请链接
        /// </summary>
        /// <param name="command">创建邀请链接命令</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>邀请码</returns>
        [CheckPermission(Permissions.ManageOrgInvitation)]
        [HttpPost]
        [ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
        [AuditLogActionFilter("创建邀请链接")]
        public async Task<ActionResult<string>> CreateInvitationAsync(
            [FromBody] CreateOrgInvitationCommand command,
            CancellationToken cancellationToken)
        {
            var result = await _mediator.Send(command, cancellationToken);

            return !result.IsSuccess ? this.CustomBadRequest(result.Message) : Ok(result.Data);
        }

        /// <summary>
        /// 删除邀请链接
        /// </summary>
        /// <param name="invitationId">邀请ID</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>操作结果</returns>
        [CheckPermission(Permissions.ManageOrgInvitation)]
        [HttpDelete("{invitationId:long:min(1)}")]
        [ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
        [AuditLogActionFilter("删除邀请链接")]
        public async Task<IActionResult> DeleteInvitationAsync(long invitationId, CancellationToken cancellationToken)
        {
            var command = new DeleteOrgInvitationCommand
            {
                InvitationId = invitationId
            };

            var result = await _mediator.Send(command, cancellationToken);

            return !result.IsSuccess ? this.CustomBadRequest(result.Message) : Ok(result.Data);
        }

        /// <summary>
        /// 获取指定组织的邀请链接列表
        /// </summary>
        /// <param name="currentUser">当前登录用户</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>邀请链接列表</returns>
        [CheckPermission(Permissions.ManageOrgInvitation)]
        [HttpGet("search")]
        [ProducesResponseType(typeof(IEnumerable<OutDutyViewModel>), StatusCodes.Status200OK)]
        [AuditLogActionFilter("查询邀请链接列表")]
        public async Task<ActionResult<IEnumerable<OutOrgInvitationViewModel>>> GetOrganizationInvitationsAsync(
            [FromServices] ICurrentUser currentUser,
            CancellationToken cancellationToken)
        {
            long orgId = QueryHelper.GetOrgId(currentUser);

            var invitations = await _organizationQueries
                .GetInvitationsAsync(orgId, cancellationToken);

            return Ok(invitations);
        }
    }
}